package xyz.liuyuhe.StudySpring.ch9.demo0;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class AccountServiceImpl implements AccountService{
    private AccountDao accountDao;
    private DataSourceTransactionManager txManager;
    
    @Override
    public void transfer(String out, String in, double money) {
        DefaultTransactionDefinition def1 = new DefaultTransactionDefinition();
        def1.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        def1.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        
        TransactionStatus status = txManager.getTransaction(def1);
        accountDao.outMoney(out, money);
        
        accountDao.inMoney(in, money);
        
        txManager.commit(status);
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void setTxManager(DataSourceTransactionManager txManager) {
        this.txManager = txManager;
    }
}
